package com.promise.jokerdream.work.signin

import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.repository.foldWithApiException
import com.promise.jokerdream.utils.AppContext
import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult

// 世界树
class WorldTreeWork(uin: String): BaseWork(uin) {

    private val gameApi = GameApi.Companion.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.Companion.getInstance(AppContext.get()) }

    override val name: String
        get() = "世界树"

    override suspend fun executeWork(): WorkResult {
        return try {
            // 执行灵宝数相关操作
            executeBlessOperations()

            // 执行源宝树相关操作
            executeSourceTreeOperations()

            WorkResult.Success
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            WorkResult.Failure(e, e.message)
        }
    }

    /**
     * 执行灵宝树相关操作
     */
    private suspend fun executeBlessOperations(): WorkResult {
        try {
            // 先查询灵宝树状态
            val queryResult = gameApi.queryBlessTree()
            return queryResult.foldWithApiException(
                onSuccess = { response ->
                    // 根据查询结果决定是否领取
                    if (response.gift == 0) {
                        // 可以领取每日奖励（材料）
                        val materialResult = gameApi.getWorldTreeMaterial()
                        materialResult.foldWithApiException(
                            onSuccess = { materialResponse ->
                                historyManager.addHistory(name, materialResponse.getDescription())
                            },
                            onFailure = { code, msg ->
                                historyManager.addHistory(name, msg, code)
                            }
                        )
                    }

                    if (response.login == 1) {
                        // 可以领取登录奖励
                        val freeExpResult = gameApi.getWorldTreeExperience(1) // 1为登录
                        freeExpResult.foldWithApiException(
                            onSuccess = { expResponse ->
//                                historyManager.addHistory(name, expResponse.getDescription())
                                // 傻逼乐斗没返回值，先不打印了
                            },
                            onFailure = { code, msg ->
                                historyManager.addHistory(name, msg, code)
                            }
                        )
                    }

                    if (response.cost == 1) {
                        // 可以领取付费奖励
                        val paidExpResult = gameApi.getWorldTreeExperience(2) // 2为鹅币
                        paidExpResult.foldWithApiException(
                            onSuccess = { expResponse ->
                                historyManager.addHistory(name, expResponse.getDescription())
                            },
                            onFailure = { code, msg ->
                                historyManager.addHistory(name, msg, code)
                            }
                        )
                    }
                    WorkResult.Success
                },
                onFailure = { code, msg ->
                    historyManager.addHistory(name, msg, code)
                    WorkResult.Failure(Exception(msg), msg)
                }
            )
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            return WorkResult.Failure(e, e.message)
        }
    }

    /**
     * 执行源宝树相关操作
     */
    private suspend fun executeSourceTreeOperations(): WorkResult {
        try {
            // 查询源宝树信息
            val queryResult = gameApi.querySourceTree()
            return queryResult.foldWithApiException(
                onSuccess = { response ->
                    if (response.freeStatus == 1) {
                        if (response.weaponId > 0) {
                            // 有武器且有免费次数，进行喂养
                            val feedResult = gameApi.feedSourceTree(response.weaponId)
                            feedResult.foldWithApiException(
                                onSuccess = { feedResponse ->
                                    historyManager.addHistory(name, feedResponse.getDescription())
                                    WorkResult.Success
                                },
                                onFailure = { code, msg ->
                                    historyManager.addHistory(name, msg, code)
                                    WorkResult.Failure(Exception(msg), msg)
                                }
                            )
                        } else {
                            historyManager.addHistory(name, "没有选中武器喂养")
                            WorkResult.Success
                        }
                    } else {
                        WorkResult.Success
                    }
                },
                onFailure = { code, msg ->
                    historyManager.addHistory(name, msg, code)
                    WorkResult.Failure(Exception(msg), msg)
                }
            )
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            return WorkResult.Failure(e, e.message)
        }
    }
}